/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.proxy.component
{
	import com.edi.runtime.component.EDIApplication;
	import com.edi.runtime.component.EDIDataSource;
	import com.edi.runtime.component.EDIDataTable;
	
	import mx.messaging.messages.ErrorMessage;
	
	/**
	 *  构造TPO语句请求串 
	 * 
	 */	
	public class TPOParam extends POParam  implements IPOParam
	{
		
		private var _cmdXml:XML;
		private var _dsName:String;
		private var _methods:String;
		private var _ediDataTable:EDIDataTable;
		
		/**
		 * 构造TPO语句请求串 
		 * <ds>
		 *	<caller/>
		 *	<appName>contract</appName>
		 *	<TPOParam>
		 *	<dsName>a11</dsName>
		 *	<methods>update</methods>
		 *	<table name="a11">
		 *	<row Operation="add">
		 *	<a1 fieldType="string">asdfasdf</a1>
		 *	<pid fieldType="integer"></pid>
		 *	<id fieldType="integer" primaryKey="1">33</id>
		 *	</row>
		 *	</table>
		 *	</TPOParam>
		 *	</ds>
		 * 
		 */		
		public function TPOParam() 
		{

		}
		
		/**
		 * 将要提交的每行信息 对应<row/> 
		 * @param value
		 * 
		 */		
		public function set cmdXml(value:XML):void{
			_cmdXml=value;
			try
			{
				_dsName=value.@name;
			}catch(e:ErrorMessage){};	
		}
		public function get cmdXml():XML{return _cmdXml;}
		
		/**
		 * 服务名；对应toXml()中的 <dsName></dsName> 
		 * @param _dsName
		 * 
		 */		
		public function set dsName(_dsName:String):void{this._dsName = _dsName;	}
		public function get dsName():String{return this._dsName;}
		
		/**
		 * 方法名；对应toXML()中的<methods/> 
		 * @param value
		 * 
		 */		
		public function set methods(value:String):void{_methods=value;}
		public function get methods():String{return _methods;}
		
		/**
		 * XML 组成xml协议串 
		 * @return 
		 * 
		 */		
		override public function toXml():XML
		{
			var pxml:XML=new XML(<TPOParam />);
			pxml.appendChild(<dsName>{dsName}</dsName>);
			pxml.appendChild(<transaction>{transaction}</transaction>);
			pxml.appendChild(<methods>{methods}</methods>);
			if(cmdXml!=null)
			{
				pxml.appendChild(cmdXml);
			}
			else
			{
				pxml.appendChild(<table name={dsName} />);
			}
			return pxml;
		}
		
		/**
		 *  新添加一样是的dataTable对象。
		 * @return 
		 * 
		 */		
		public function get dataTable():EDIDataTable
		{
			return _ediDataTable;
		}
		
		/**
		 * 新添加一样 
		 * @param autoDispatch 是否怎东分发
		 * @param ds  分发到得数据集
		 * @param resultCallBack  成功后的函数回调
		 * 
		 */		
		public function newRow(autoDispatch:Boolean=false,ds:EDIDataSource=null,resultCallBack:Function=null):void
		{
			var proxy:HttpProxy=new HttpProxy(EDIApplication.appName,autoDispatch,ds);
			this.methods="newRow";
			proxy.addPoParam(this);
			if(!autoDispatch)
			{
				proxy.resultCallback=function(event:HttpProxyResult):void
				{
					var tableXml:XML=event.getPoResultByDsName(dsName).dataSource
					if(ds!=null)
					{
						ds.addDataTable(tableXml,autoDispatch)
					}
					else
					{
						_ediDataTable=new EDIDataTable(XML(tableXml.table[0]).children());
						_ediDataTable.newRowState=tableXml.newRow;
						_ediDataTable.dataSourceName=tableXml.dsName[0];
					}
					if(resultCallBack!=null)
					{
						resultCallBack.call();
					}
				}	
			}
			else
			{
				proxy.resultCallback=function(event:HttpProxyResult):void
				{
					if(resultCallBack!=null)
					{
						resultCallBack.call();
					}
				}
			}
			proxy.send();
		} 
		
	}
}